package com.wxdc.nmgyt.nmgytwydc.presenter;

import com.wxdc.nmgyt.nmgytwydc.base.RxPersenter;
import com.wxdc.nmgyt.nmgytwydc.http.RetrofitHelper;
import com.wxdc.nmgyt.nmgytwydc.model.PicUploadStateBean;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.PhotoUploadContract;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/6/12.
 */

public class PhotoUploadPersenter extends RxPersenter<PhotoUploadContract.View> implements PhotoUploadContract.Persenter {
    private RetrofitHelper mRetrofitHelper;
    @Inject
    public PhotoUploadPersenter(RetrofitHelper retrofitHelper) {
        mRetrofitHelper = retrofitHelper;
    }

    /*
    *
    * 上传指定数量的图片
    * */
    @Override
    public void photoUploads(List<String> picUrls) {
        Map<String, RequestBody> bodyMap = new HashMap<>();
        int picNum = picUrls.size();
        if (picNum > 0) {
            for (int i = 0; i < picNum;i++) {
                File file = new File(picUrls.get(i));
                bodyMap.put("file" + i+"\";filename=\"" + file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
            }
        }
        mRetrofitHelper.multiUpLoadPics(bodyMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PicUploadStateBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<PicUploadStateBean> picUploadStateBeans) {
                       // mView.photoUploadResult(picUploadStateBeans);
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @Override
    public void photoUpload(String picUrl) {
        File imgFile = new File(picUrl);
        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/png"), imgFile);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("uploaded_file_pic", imgFile.getName(), requestFile);

        mRetrofitHelper.singleUpLoadPic(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PicUploadStateBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(PicUploadStateBean picUploadStateBeans) {
                        mView.photoUploadResult(picUploadStateBeans);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}